package com.simple;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.*;

public class SPThreadPoolExecutor extends AbstractExecutorService {

    private int corePoolSize;
    private int maxPoolSize;
    private long timeout;
    private BlockingQueue blockingQueue;
    private ThreadFactory threadFactory;
    private List<MyThread> works;
    private int totalWork = 0;

    public SPThreadPoolExecutor(int corePoolSize, int maxPoolSize, TimeUnit unit,long timeout, BlockingQueue blockingQueue, ThreadFactory threadFactory) {
        this.corePoolSize = corePoolSize;
        this.maxPoolSize = maxPoolSize;
        this.timeout = unit.toNanos(timeout);
        this.blockingQueue = blockingQueue;
        this.threadFactory = threadFactory;
        works = new ArrayList<>(corePoolSize);
        for (int i = 0; i < corePoolSize; i++) {
            works.add(new MyThread());
        }
    }

    @Override
    public void shutdown() {

    }

    @NotNull
    @Override
    public List<Runnable> shutdownNow() {
        return null;
    }

    @Override
    public boolean isShutdown() {
        return false;
    }

    @Override
    public boolean isTerminated() {
        return false;
    }

    @Override
    public boolean awaitTermination(long timeout, @NotNull TimeUnit unit) throws InterruptedException {
        return false;
    }

    @Override
    public void execute(@NotNull Runnable command) {
        totalWork++;
        if (totalWork <= corePoolSize){
            MyThread thread = works.get(totalWork - 1);
            thread.setTarget(command);
        }
    }

    static class MyThread extends Thread{
        Runnable target;

        public void setTarget(Runnable target) {
            this.target = target;
        }

        @Override
        public void run() {
            while (this.target != null){
                this.target.run();
                return;
            }
        }
    }
}
